package study.core.rdd

import org.apache.spark.{SparkConf, SparkContext}

/**
 * kryo序列化
 *
 * @author zh
 * @date 2021/5/17 10:01
 */
object TestKryo {
  def main(args: Array[String]): Unit = {

    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("kryo")
      // 默认为org.apache.spark.serializer.JavaSerializer
      // 如果注册了org.apache.spark.serializer.KryoSerializer
//      .set("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .registerKryoClasses(Array(classOf[Teacher]))

    val context = new SparkContext(sparkConf)

    val teacher:Teacher = new Teacher
    teacher.name = "张三"
    teacher.age = 22
    teacher.sex = "男"

    val teacher1:Teacher = new Teacher
    teacher1.name = "小兰"
    teacher1.age = 24
    teacher1.sex = "女"

    val rdd = context.makeRDD(List(teacher,teacher1))

    // 根据性别统计
    val sexRdd = rdd.map(
      tea => (tea.sex, 1)
    )

    val result = sexRdd.groupByKey()
    result.collect().foreach(println)


  }

  class Teacher
//    extends Serializable
  {
    var name:String =_
    var age:Int = _
    var sex:String = _
  }

}
